package com.ruoyi.baseData.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.Material;
import com.ruoyi.baseData.domain.ProcessRel;
import com.ruoyi.baseData.domain.dto.ProcessDto;
import com.ruoyi.baseData.domain.vo.ProcessVo;
import com.ruoyi.baseData.domain.Process;
import com.ruoyi.baseData.mapper.MaterialMapper;
import com.ruoyi.baseData.mapper.ProcessMapper;
import com.ruoyi.baseData.service.ProcessService;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
*
*/
@Service
public class ProcessServiceImpl implements ProcessService {
    @Resource
    ProcessMapper processMapper;
    @Resource
    MaterialMapper materialMapper;
    @Override
    public Page<ProcessVo> getProcessByPage(ProcessDto pageEntity) {

        MPJLambdaWrapper<Process> wrapper = new MPJLambdaWrapper<Process>()
                .selectAll(Process.class)//查询user表全部字段
                .setAlias("materialName")
                .selectAs( Material::getName, ProcessVo::getMaterialName )
                .setAlias("materialCode")
                .selectAs( Material::getCode, ProcessVo::getMaterialCode )
                //记的测试阶段
                .leftJoin(Material.class, Material::getId, Process::getMaterialId)
                .eq(Process::getDelFlag, 0)
                .orderByDesc(Process::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, Process.class, pageEntity);
        Page<ProcessVo> userList = processMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   ProcessVo.class, wrapper);
        return userList;
    }

    @Override
    public void addProcess(ProcessDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<Process> wrapper = new MPJLambdaWrapper<Process>()
                .eq(Process::getCode, testDto.getCode())
                .ne(Process::getDelFlag, 1);
        Process process = processMapper.selectOne(wrapper);
        Material material = materialMapper.selectById(testDto.getMaterialId());
        if(null != process) {
            throw new GlobalException("该Process已经存在");
        }
        Process test2 = new Process();
        BeanUtils.copyProperties(testDto, test2,  new String[]{"id"});
        test2.setDelFlag(false);
        test2.setMaterialId(material.getId());
        test2.setVersion("A");
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        processMapper.insert(test2);
    }

    @Override
    public void updateProcess(ProcessDto testDto) {
        MPJLambdaWrapper<Process> wrapper = new MPJLambdaWrapper<Process>()
                .eq(Process::getCode, testDto.getCode());
        Process material = processMapper.selectOne(wrapper);
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        BeanUtils.copyProperties(testDto, material);
        material.setCreateBy("admin");
        material.setCreateTime(new Date());
        processMapper.updateById(material);
    }

    @Override
    public void delProcess(ProcessDto testDto) {
        Process material = processMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        material.setDelFlag(true);
        processMapper.updateById(material);
    }
}
